﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using ANGFLib;

namespace ANGF
{
    class ScenarioRecord
    {
        internal string ModuleUri;
    }

    class CloudServer
    {
        private static WebApiResults SendRequest(WebApiParameters parameters)
        {
            try
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(WebApiParameters));

                MemoryStream ms = new MemoryStream();
                serializer.WriteObject(ms, parameters);
                string JsonString = Encoding.UTF8.GetString(ms.ToArray());

                var http = new WebClient();
                var r = http.UploadString("https://angfwebplayer4.azurewebsites.net/Api/Common", JsonString);

                byte[] bytes = Encoding.UTF8.GetBytes(r);
                MemoryStream ms2 = new MemoryStream(bytes);
                return (WebApiResults)serializer.ReadObject(ms2);
            }
            catch (Exception)
            {
                // TBW エラー処理は考える
                return null;
            }
        }

        internal static IEnumerable<ScenarioRecord> GetModules(RegistryMiscInfo misc)
        {
            var p = new WebApiParameters();
            p.Command = WebCommands.DownloadModules;
            var r = SendRequest(p);
            foreach (var item in r.Urls)
            {
                yield return new ScenarioRecord() { ModuleUri = item };
            }
        }
    }
}
